#include "cdesktop.h"
#include "cbgdialog.h"

#include <buola/dbus/cproxy.h>
#include <buola/algorithm/container.h>
#include <buola/gui/cmenu.h>
#include <buola/util/ccolordialog.h>
#include <buola/io/cfolder.h>
#include <buola/util/cfontdialog.h>
#include <buola/gui/ctoolkit.h>
#include <buola/image/cimage.h>
#include <buola/os/cprocess.h>
#include <buola/utility/crandom.h>
#include <buola/image/cgc.h>

#define ID_TERM         1
#define ID_SETTINGS     3
#define ID_COLOR        4
#define ID_FONT         5

namespace buola { namespace bdesktop {

CDesktop::CDesktop()
{
    mBgDialog=nullptr;
    mCurrentImage=nullptr;
    mCurrent=-1;
    mTimer=nullptr;
}

CDesktop::~CDesktop()
{
}

void CDesktop::SetNextBack()
{
    int lCount=mWallpapers.size();
    if(!lCount) return;

    int lNext=(mCurrent+1)%lCount;
    if(lNext==mCurrent) return;
    PImage lNextImage=CImage::GetURL(mWallpapers[lNext]->mURL);

    if(lNextImage)
    {
        lNextImage->Scale(CIntSize(mScreenSize));
        mCurrentImage=lNextImage;
    }

    mCurrent=lNext;
}

void CDesktop::SetRandomBack()
{
    int lCount=mWallpapers.size();
    if(!lCount) return;

    int lNext=CRandom::Get(lCount);
    if(lNext==mCurrent) return;
    PImage lNextImage=CImage::GetURL(mWallpapers[lNext]->mURL);

    if(lNextImage)
    {
        lNextImage->Scale(CIntSize(mScreenSize));
        mCurrentImage=lNextImage;
    }

    mCurrent=lNext;
}

void CDesktop::OnCreate()
{
    SetCursor(gui::CCursor::STOCK_DEFAULT);

    mScreenSize=gui::get_toolkit()->GetRootWindow()->GetSize();
    MoveResize(CPoint(0,0),mScreenSize);

    SetWindowClass(CLASS_DESKTOP);

    SetMargins(CRect(0,30,0,0));

    SetIconSize(gui::CIcon::LARGE);
    SetStyleBit(CListBox::STYLE_INVERTTEXT);

    CFileList::OnCreate();

//  PFolder lFolder=CFolder::Get("/usr/share/wallpapers/National Geographic");
    PFolder lFolder=CFolder::Get("/usr/share/wallpapers");
    std::vector<PFile> lFiles;
    lFolder->GetAll(lFiles);

    for(int i=0;i<lFiles.size();i++)
    {
        if(lFiles[i]->IsFolder()) continue;

        if(lFiles[i]->GetURL().GetExtension()!="jpg") continue;

        SWallpaper &lWallpaper=construct_back(mWallpapers);
        lWallpaper.mURL=lFiles[i]->GetURL();
    }

    msg_info() << mWallpapers.size() << "\n";

    mCurrent=-1;

    SetRandomBack();
    SetURL("/home/xavi/Desktop");

    Refresh();

    mTimer=new CTimer(std::chrono::minutes(5));
    mTimer->Enable();
    mTimer->sTimer.Connect(&CDesktop::OnTimer,this);

    eIntAction.Connect(&CDesktop::OnIntAction,this,_1,_2);
}

void CDesktop::OnFillMenu(gui::PMenu pMenu,bool pItem)
{
    CFileList::OnFillMenu(pMenu,pItem);

    if(!pItem)
    {
        pMenu->AddSeparator();
        pMenu->Add(gui::new_menu_item(WI18N("New terminal")))->eTriggered.Connect(&CDesktop::OnTerm,this);
        pMenu->Add(gui::new_menu_item(WI18N("Launch bwm")))->eTriggered.Connect(&CDesktop::OnLaunchWM,this);
        pMenu->Add(gui::new_menu_item(WI18N("Settings...")))->eTriggered.Connect(&CDesktop::OnSettings,this);
    }
}

void CDesktop::OnTerm()
{
    CProcess lProcess("bterm",CProcess::DETACH|CProcess::SEARCH_PATH);
    lProcess.Launch();
}

void CDesktop::OnLaunchWM()
{
    CProcess lProcess("bwm",CProcess::DETACH|CProcess::SEARCH_PATH);
    lProcess.Launch();
}

void CDesktop::OnSettings()
{
    if(!mBgDialog)
    {
        mBgDialog=new CBgDialog(this);
        mBgDialog->Create(nullptr);
        mBgDialog->Show();
    }
}

void CDesktop::OnExpose(gui::CExposeEvent *pE)
{
    if(mCurrentImage)
        pE->mGC->DrawImage(CPoint(0,0),mCurrentImage);
    CFileList::OnExpose(pE);
}

void CDesktop::OnTimer()
{
    SetRandomBack();
    Refresh();
}

void CDesktop::OnIntAction(CFileInterface::EIntAction pAction,const CURL &pURL)
{
    if(pAction==CFileInterface::OPEN_URL)
    {
        dbus::CProxy lProxy(dbus::get_session_bus(),"org.buola.Browser","/org/buola/Browser");
        lProxy.CallIgnore("org.buola.Browser","NewWindow",pURL);
    }
}

/*namespace bdesktop*/ } /*namespace buola*/ }
